<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <h1>Hello</h1>
          <h2>欢迎来到我的管理系统</h2>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              :loading="loading"
              type="primary"
              size="default"
              class="login_btn"
              @click="login"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import userUserStore from '@/store/modules/user'
import { getTime } from '@/utils/time'

let loginForm = reactive({ username: 'admin', password: 'atguigu123' })

let userStore = userUserStore()
let loginForms = ref()
let $router = useRouter()
let $route = useRoute()

let loading = ref(false)
const login = async () => {
  await loginForms.value.validate()

  loading.value = true
  try {
    // 保证登录成功后才能进入系统
    await userStore.userLogin(loginForm)
    // 编程式导航跳转到展示数据

    let redirect: any = $route.query.redirect

    $router.push({ path: redirect || '/' })

    ElNotification({
      message: '登录成功!',
      type: 'success',
      title: `HI,${getTime()}`,
    })
  } catch (error) {
    ElNotification({
      message: (error as Error).message,
      type: 'error',
    })
  } finally {
    loading.value = false
  }
}

const validatorUserName = (_rule: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('账号长度最少五位'))
  }
}
const validatorPassword = (_rule: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少是6位'))
  }
}
const rules = reactive({
  username: [
    // { required: true, message: '请输入用户名', trigger: 'blur' },
    // {
    //   required: true,
    //   min: 6,
    //   max: 10,
    //   message: '账号长度至少为六位',
    //   trigger: 'blur',
    // },

    // 自定义字段校验
    {
      trigger: 'change',
      validator: validatorUserName,
    },
  ],
  password: [
    // {
    //   required: true,
    //   min: 6,
    //   max: 15,
    //   message: '密码长度至少为六位',
    //   trigger: 'blur',
    // },
    { trigger: 'blur', validator: validatorPassword },
  ],
})
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  block-size: cover;

  .login_form {
    position: relative;
    width: 80%;
    top: 30vh;
    background: url('@/assets/images/login_form.png');
    padding: 30px;
  }

  h1 {
    color: white;
    font-size: 40px;
  }

  h2 {
    color: white;
    font-size: 20px;
    margin: 20px 0px;
  }

  .login_btn {
    width: 100%;
  }
}
</style>
